﻿using MediatR;
using Traceability.Commont.Domains;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.DTOs.SectionDto;
using Traceability.Commont.Infrastructures.Interfaces;

namespace U.Enterprise.Api.Applications.GETSection
{
    public class SectionHtmlHandler : IRequestHandler<SectionHtmlCommond, SectiongetDto>
    {


        private readonly IBateRepository<Department> _Depatment;

        public SectionHtmlHandler(IBateRepository<Department> depatment)
        {
            _Depatment = depatment;
        }
        /// <summary>
        /// 部门显示页面
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<SectiongetDto> Handle(SectionHtmlCommond request, CancellationToken cancellationToken)
        {
            var res = new SectiongetDto() { count=0,NEWsections=new List<NEWsection>(),page=0 };

            var process = new SectiongetDto();


               var list =( from a in _Depatment.GetAll()
                         select new NEWsection()
                         {
                            Departmentid = a.Departmentid,
                            DepartmentName=a.DepartmentName,
                             Levelid=a.Levelid,
                             Typeid=a.Typeid,

                         }).ToList();




          


            if(!string.IsNullOrEmpty(request.SectionName))
            {
                list=list.Where(x=>x.DepartmentName.Contains(request.SectionName)).ToList();
            }
            if (request.SectionType!=0) 
            {
                list = list.Where(x => x.Typeid==request.SectionType).ToList();
            }
            //if (request.SectionStatus != 0)
            //{
            //    Put = Put.Where(x => x. == request.SectionType).ToList();
            //}

            res.count = list.Count;
            res.page = (int)Math.Ceiling(list.Count * 1.0 / request.pagesize);


            list=list.OrderBy(x=>x.Departmentid).Skip((request.pageindex - 1)*request.pagesize).Take(request.pagesize).ToList();

            if(list!=null)
            {
                res.NEWsections = list;

               
            }
            
            return res;



        }
    }
}
